package cn.iocoder.yudao.module.product.controller.admin.conveniencestore;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.product.controller.admin.conveniencestore.vo.ConvenienceStoreMyRespVO;
import cn.iocoder.yudao.module.product.controller.admin.conveniencestore.vo.ConvenienceStorePageReqVO;
import cn.iocoder.yudao.module.product.controller.admin.conveniencestore.vo.ConvenienceStoreRespVO;
import cn.iocoder.yudao.module.product.controller.admin.conveniencestore.vo.ConvenienceStoreSaveReqVO;
import cn.iocoder.yudao.module.product.dal.dataobject.conveniencestore.ConvenienceStoreDO;
import cn.iocoder.yudao.module.product.service.conveniencestore.ConvenienceStoreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 商铺")
@RestController
@RequestMapping("/product/convenience-store")
@Validated
public class ConvenienceStoreController {

    @Resource
    private ConvenienceStoreService convenienceStoreService;

    @PostMapping("/create")
    @Operation(summary = "创建商铺")
    @PreAuthorize("@ss.hasPermission('product:convenience-store:create')")
    public CommonResult<Long> createConvenienceStore(@Valid @RequestBody ConvenienceStoreSaveReqVO createReqVO) {
        return success(convenienceStoreService.createConvenienceStore(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商铺")
    @PreAuthorize("@ss.hasPermission('product:convenience-store:update')")
    public CommonResult<Boolean> updateConvenienceStore(@Valid @RequestBody ConvenienceStoreSaveReqVO updateReqVO) {
        convenienceStoreService.updateConvenienceStore(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商铺")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:convenience-store:delete')")
    public CommonResult<Boolean> deleteConvenienceStore(@RequestParam("id") Long id) {
        convenienceStoreService.deleteConvenienceStore(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商铺")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:convenience-store:query')")
    public CommonResult<ConvenienceStoreRespVO> getConvenienceStore(@RequestParam("id") Long id) {
        ConvenienceStoreDO convenienceStore = convenienceStoreService.getConvenienceStore(id);
        return success(BeanUtils.toBean(convenienceStore, ConvenienceStoreRespVO.class, convenienceStoreVO -> {
            convenienceStoreVO.setAreaName(AreaUtils.format(convenienceStoreVO.getAreaId()));
            convenienceStoreVO.setDoorHeaderImage(convenienceStore.getDoorHeaderImage().split(","));
        }));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商铺分页")
    @PreAuthorize("@ss.hasPermission('product:convenience-store:query')")
    public CommonResult<PageResult<ConvenienceStoreRespVO>> getConvenienceStorePage(@Valid ConvenienceStorePageReqVO pageReqVO) {
        PageResult<ConvenienceStoreDO> pageResult = convenienceStoreService.getConvenienceStorePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConvenienceStoreRespVO.class, convenienceStoreVO -> {
            convenienceStoreVO.setAreaName(AreaUtils.format(convenienceStoreVO.getAreaId()));
        }));
    }

    @GetMapping("/myShopList")
    @Operation(summary = "获得商铺分页")
    @PreAuthorize("@ss.hasPermission('product:convenience-store:query')")
    public CommonResult<List<ConvenienceStoreMyRespVO>> getMyShopList() {
        Long userId = WebFrameworkUtils.getLoginUserId();
        List<ConvenienceStoreDO> convenienceStoreList = convenienceStoreService.getMyConvenienceStoreList(userId);
        return success(BeanUtils.toBean(convenienceStoreList, ConvenienceStoreMyRespVO.class));
    }

    @PutMapping("/submit")
    @Operation(summary = "提交合同审批")
    @PreAuthorize("@ss.hasPermission('crm:convenience-store:update')")
    public CommonResult<Boolean> submitConvenienceStore(@RequestParam("id") Long id) {
        convenienceStoreService.submitConvenienceStore(id, getLoginUserId());
        return success(true);
    }

    //
    //@GetMapping("/export-excel")
    //@Operation(summary = "导出商铺 Excel")
    //@PreAuthorize("@ss.hasPermission('product:convenience-store:export')")
    //@ApiAccessLog(operateType = EXPORT)
    //public void exportConvenienceStoreExcel(@Valid ConvenienceStorePageReqVO pageReqVO,
    //          HttpServletResponse response) throws IOException {
    //    pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
    //    List<ConvenienceStoreDO> list = convenienceStoreService.getConvenienceStorePage(pageReqVO).getList();
    //    // 导出 Excel
    //    ExcelUtils.write(response, "商铺.xls", "数据", ConvenienceStoreRespVO.class,
    //                    BeanUtils.toBean(list, ConvenienceStoreRespVO.class));
    //}

}